package com.up.upzl.biz.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.up.upzl.biz.domain.Captcha;
import com.up.upzl.biz.service.CaptchaService;
import com.up.upzl.biz.service.paiDianRecordRepositorys;

/**
 * 用户控制器
 * 
 * @author geeks 岳思伟
 * @param
 */
@RestController
@RequestMapping("/")
public class UserController {

	private final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private CaptchaService captchaService;

	@Autowired
	private paiDianRecordRepositorys paiDianRecordRepositorys;

	/**
	 * 获取验证码
	 * 
	 * @throws Exception
	 */
	@PostMapping("/getCaptcha")
	public int getCaptcha1(HttpServletRequest request, HttpSession session) throws Exception {
		String datas = request.getParameter("val");
		String data = datas.replaceAll("-", "");
		String openid = session.getAttribute("openid").toString();
		log.info("获取验证的手机号码：{}和openid:{}", data, openid);
		Integer code = captchaService.getCaptcha(data, openid);
		if (code != null) {
			return 1;
		} else {
			return 0;
		}
	}

	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		String ihones = request.getParameter("ihone");
		// 拍店人手机号码
		String phone = ihones.replaceAll("-", "");
		String code = request.getParameter("code");
		Captcha captcha = captchaService.findCode(phone, code);
		session.setAttribute("phone", phone);
		if (captcha != null) {
			return "1";
		}
		return "0";
	}

}
